| Conditions | 1 |
| Paths | 2 |
| Total Lines | 102 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | (function() { |
||
| 3 | d3.hexbin = function() { |
||
|
|
|||
| 4 | var width = 1, |
||
| 5 | height = 1, |
||
| 6 | r, |
||
| 7 | x = d3_hexbinX, |
||
| 8 | y = d3_hexbinY, |
||
| 9 | dx, |
||
| 10 | dy; |
||
| 11 | |||
| 12 | function hexbin(points) { |
||
| 13 | var binsById = {}; |
||
| 14 | |||
| 15 | points.forEach(function(point, i) { |
||
| 16 | var py = y.call(hexbin, point, i) / dy, pj = Math.round(py), |
||
| 17 | px = x.call(hexbin, point, i) / dx - (pj & 1 ? .5 : 0), pi = Math.round(px), |
||
| 18 | py1 = py - pj; |
||
| 19 | |||
| 20 | if (Math.abs(py1) * 3 > 1) { |
||
| 21 | var px1 = px - pi, |
||
| 22 | pi2 = pi + (px < pi ? -1 : 1) / 2, |
||
| 23 | pj2 = pj + (py < pj ? -1 : 1), |
||
| 24 | px2 = px - pi2, |
||
| 25 | py2 = py - pj2; |
||
| 26 | if (px1 * px1 + py1 * py1 > px2 * px2 + py2 * py2) pi = pi2 + (pj & 1 ? 1 : -1) / 2, pj = pj2; |
||
| 27 | } |
||
| 28 | |||
| 29 | var id = pi + "-" + pj, bin = binsById[id]; |
||
| 30 | if (bin) bin.push(point); else { |
||
| 31 | bin = binsById[id] = [point]; |
||
| 32 | bin.i = pi; |
||
| 33 | bin.j = pj; |
||
| 34 | bin.x = (pi + (pj & 1 ? 1 / 2 : 0)) * dx; |
||
| 35 | bin.y = pj * dy; |
||
| 36 | } |
||
| 37 | }); |
||
| 38 | |||
| 39 | return d3.values(binsById); |
||
| 40 | } |
||
| 41 | |||
| 42 | function hexagon(radius) { |
||
| 43 | var x0 = 0, y0 = 0; |
||
| 44 | return d3_hexbinAngles.map(function(angle) { |
||
| 45 | var x1 = Math.sin(angle) * radius, |
||
| 46 | y1 = -Math.cos(angle) * radius, |
||
| 47 | dx = x1 - x0, |
||
| 48 | dy = y1 - y0; |
||
| 49 | x0 = x1, y0 = y1; |
||
| 50 | return [dx, dy]; |
||
| 51 | }); |
||
| 52 | } |
||
| 53 | |||
| 54 | hexbin.x = function(_) { |
||
| 55 | if (!arguments.length) return x; |
||
| 56 | x = _; |
||
| 57 | return hexbin; |
||
| 58 | }; |
||
| 59 | |||
| 60 | hexbin.y = function(_) { |
||
| 61 | if (!arguments.length) return y; |
||
| 62 | y = _; |
||
| 63 | return hexbin; |
||
| 64 | }; |
||
| 65 | |||
| 66 | hexbin.hexagon = function(radius) { |
||
| 67 | if (arguments.length < 1) radius = r; |
||
| 68 | return "m" + hexagon(radius).join("l") + "z"; |
||
| 69 | }; |
||
| 70 | |||
| 71 | hexbin.centers = function() { |
||
| 72 | var centers = []; |
||
| 73 | for (var y = 0, odd = false, j = 0; y < height + r; y += dy, odd = !odd, ++j) { |
||
| 74 | for (var x = odd ? dx / 2 : 0, i = 0; x < width + dx / 2; x += dx, ++i) { |
||
| 75 | var center = [x, y]; |
||
| 76 | center.i = i; |
||
| 77 | center.j = j; |
||
| 78 | centers.push(center); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | return centers; |
||
| 82 | }; |
||
| 83 | |||
| 84 | hexbin.mesh = function() { |
||
| 85 | var fragment = hexagon(r).slice(0, 4).join("l"); |
||
| 86 | return hexbin.centers().map(function(p) { return "M" + p + "m" + fragment; }).join(""); |
||
| 87 | }; |
||
| 88 | |||
| 89 | hexbin.size = function(_) { |
||
| 90 | if (!arguments.length) return [width, height]; |
||
| 91 | width = +_[0], height = +_[1]; |
||
| 92 | return hexbin; |
||
| 93 | }; |
||
| 94 | |||
| 95 | hexbin.radius = function(_) { |
||
| 96 | if (!arguments.length) return r; |
||
| 97 | r = +_; |
||
| 98 | dx = r * 2 * Math.sin(Math.PI / 3); |
||
| 99 | dy = r * 1.5; |
||
| 100 | return hexbin; |
||
| 101 | }; |
||
| 102 | |||
| 103 | return hexbin.radius(1); |
||
| 104 | }; |
||
| 105 | |||
| 111 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.